import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const commonRoutes = [
    {
        path: '/login',
        name: 'login',
        meta: { title: '登录' },
        component: () => import('../components/Login.vue'),
    },
    {
        path: '/register',
        name: 'register',
        meta: { title: '注册' },
        component: () => import('../components/Register.vue'),
    },
    {
        path: '/404',
        name: '404',
        meta: { title: '404' },
        component: () => import('../components/404.vue'),
    },
    { path: '/admin', redirect: '/home' },
]

const webRoutes = [
    {
        path: '/',
        name: 'index',
        meta: { title: '博客首页' },
        component: () => import('../views/web/Blog.vue'),
    },
    {
        path: '/detail',
        name: 'detail',
        meta: { title: '博客详情' },
        component: () => import('../views/web/Detail.vue'),
    },
]

// 本地所有的页面 需要配合后台返回的数据生成页面
export const asyncRoutes = {
    home: {
        path: 'home',
        name: 'home',
        meta: { title: '主页' },
        component: () => import('../views/admin/Home.vue'),
    },
    category: {
        path: 'category',
        name: 'category',
        meta: { title: '分类目录' },
        component: () => import('../views/admin/Category.vue'),
    },
    article: {
        path: 'article',
        name: 'article',
        meta: { title: '全部文章' },
        component: () => import('../views/admin/Article.vue'),
    },
    post: {
        path: 'post',
        name: 'post',
        meta: { title: '写文章' },
        component: () => import('../views/admin/Post.vue'),
    },
    tag: {
        path: 'tag',
        name: 'tag',
        meta: { title: '写文章' },
        component: () => import('../views/admin/Tag.vue'),
    },
    userInfo: {
        path: 'userInfo',
        name: 'userInfo',
        meta: { title: '个人资料' },
        component: () => import('../views/admin/UserInfo.vue'),
    },
}

const createRouter = () => new Router({
    routes: [...commonRoutes, ...webRoutes],
})

const router = createRouter()

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher
}

export default router
